import { _decorator, Node, size, Size, UITransform } from 'cc';

import { UniformScaleBase } from './UniformScaleBase';

const { ccclass, property } = _decorator;

/**
 * 等比缩放节点, 保证节点能在当前分辨率下完整展示
 */
@ccclass('UniformScalePanel')
export class UniformScalePanel extends UniformScaleBase {

    @property({
        tooltip: "true：最大化等比适配UI；false：按照设计分辨率等比适配UI。"
    })
    private readonly maxFit: boolean = false;

    @property({
        tooltip: "UI适配边距，默认0。",
        visible() {
            return this.maxFit;
        }
    })
    private readonly fitBorder: number = 0;

    // 当前节点内容尺寸
    private get nodeSize(): Size {
        let size: Size = Size.ZERO;
        if (!this.active)
            return size;
        const uf = this.node.getComponent(UITransform);
        if (uf)
            size = uf.contentSize;
        return size;
    }

    // 当前节点是否允许最大化缩放展示
    private get maxFixValid(): boolean {
        return this.maxFit && !this.nodeSize.equals(Size.ZERO);
    }

    protected doUniformScale() {
        if (!this.active)
            return;

        if (this.maxFixValid) {
            const nodeSize = this.nodeSize;
            const sizeFix: Size = size(nodeSize.width + this.fitBorder * 2, nodeSize.height + this.fitBorder * 2);
            this.scale = this.getScaleMin(sizeFix);
        }
        else
            this.scaleRelative = this.getScaleMin(this.designSize);
    }

}

